// components/top/top.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    glist: Object
  },
  /**
 * 组件的初始数据
 */
  data: {
    movies: [],
    list: {
      dbName: 'movies',
      pageIndex: 1,
      pageSize: 15,
      filter: ".*",
      orderBy: 'rate',
      state: 'desc'
    },
    windowHeight: "",
  },
  ready: function () {
    let that = this;
    wx.getSystemInfo({
      complete: (res) => {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    this.requestForData1();
  },
// 监听获取的电影参数
  observers: {
    'glist': function (val) {
      let klist = this.data.list
      klist.filter = val.filter
      this.setData({
        list: klist
      })
      this.requestForData1();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 数据请求------附加
    requestForData: function () {
      wx.showLoading({
        title: '正在加载',
      })
      let that = this;
      wx.cloud
        .callFunction({
          name: 'movielist',
          data: {
            dbName: that.data.list.dbName,
            pageIndex: that.data.list.pageIndex,
            pageSize: that.data.list.pageSize,
            filter: that.data.list.filter,
            orderBy: that.data.list.orderBy,
            state: that.data.list.state
          },
        })
        .then((res) => {
          let tmp = that.data.movies.concat(res.result.data)


          tmp.forEach(item => {
            if (item.author != null) {
              item = item.author.substring(0, 20);
            }
            else {
              item.author = "暂无介绍"
            }
          })
          tmp.forEach(item => {
            if (item.director != null) {
              item = item.director.substring(0, 20);
            }
            else {
              item.director = "暂无介绍"
            }
          })
          that.setData({
            movies: tmp
          });
          wx.hideLoading()
        });
    },

    // 数据请求-----覆盖
    requestForData1: function () {
      wx.showLoading({
        title: '正在加载',
      })
      let that = this;
      wx.cloud
        .callFunction({
          name: 'movielist',
          data: {
            dbName: that.data.list.dbName,
            pageIndex: that.data.list.pageIndex,
            pageSize: that.data.list.pageSize,
            filter: that.data.list.filter,
            orderBy: that.data.list.orderBy,
            state: that.data.list.state
          },
        })
        .then((res) => {
          let tmp = res.result.data


          tmp.forEach(item => {
            if (item.author != null) {
              item = item.author.substring(0, 20);
            }
            else {
              item.author = "暂无介绍"
            }
          })
          tmp.forEach(item => {
            if (item.director != null) {
              item = item.director.substring(0, 20);
            }
            else {
              item.director = "暂无介绍"
            }
          })
          that.setData({
            movies: tmp
          });
          wx.hideLoading()
        });
    },
    // 加载更多
    loadmore: function () {
      this.data.list.pageIndex += 1
      this.requestForData();
    },

    //跳转详情
    todetail: function (options) {
      wx.navigateTo({
        url: '/pages/comm/detail/detail?id=' + options.currentTarget.dataset.id,
      })

    },

    //获取界面高度
    getPageHight: function () {
      let that = this;
      wx.getSystemInfo({
        complete: (res) => {
          that.setData({
            windowHeight: res.windowHeight
          })
        }
      })
    }

  },


})
